package com.google.code.jmarias.client.phases;

import com.google.code.jmarias.client.Entity;
import com.google.code.jmarias.client.Globals;
import com.google.code.jmarias.client.GameLocals;
import com.google.code.jmarias.client.components.ConfirmPickedComponent;
import com.google.code.jmarias.client.components.GrabCardsComponent;
import com.google.code.jmarias.client.components.PickCardComponent;
import com.google.code.jmarias.client.tcp.RemoteServer;
import com.google.code.jmarias.common.Card;
import com.google.code.jmarias.common.action.PickedTrumpAction;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;

/**
 *
 * @author bart
 */
public class PickTrumpPhase extends AbstractPhase {
    private ArrayList<Entity> actionButtons = new ArrayList<Entity>();
    private List<Class> allowedActions = new LinkedList<Class>();

    private ArrayList<Vector2f> cardsPositions = new ArrayList<Vector2f>();

    public PickTrumpPhase(RemoteServer server, Globals globals, GameLocals locals, List<Class> allowedActions) throws SlickException {
        this.server = server;
        this.globals = globals;
        this.locals = locals;
        this.allowedActions = allowedActions;

        // Odstraneni defaultni akce ze seznamu
        allowedActions.remove(PickedTrumpAction.class);

        // Vynuluji ID zmacknuteho tlacitka
        globals.setHoverID(0);
        // Vypocitam pozice pro jednotlive karty v ruce
        cardsPositions = locals.getPositionsInHand(locals.getCardsEntities().size(), locals.getHandPosition());


        // Vytvoreni tlacitek
        actionButtons = createActionButtons(allowedActions);


        // Vytvorim seznam seriovych komponent
        serialComponents = new LinkedList();
        // Naplneni fronty seriovych komponent tak, jak maji nasledovat po sobe
        this.addSerialComponent(new PickCardComponent(0, globals, locals, cardsPositions, locals.getCardsEntities()));
        this.addSerialComponent(new ConfirmPickedComponent(0, globals, locals, locals.getCardsEntities()));
        this.addSerialComponent(new GrabCardsComponent(0, cardsPositions, locals.getCardsEntities()));


        locals.getHandEntity().AddComponent(serialComponents.remove());

    }

    public void update(GameContainer gc, StateBasedGame sb, int delta) {
        Input input = gc.getInput();



        if(locals.getHandEntity().getComponent(0) == null) {

            if(serialComponents.size() > 0) {
                 System.out.println("Pridavam " + serialComponents.peek());
                 locals.getHandEntity().AddComponent(serialComponents.remove());
            } else {
                 // Nezbyly zadne komponenty k zpracovani - nastava konec faze.
                 locals.setCurrentPhase(null);
                 // Posilam serveru vybranou kartu
                 Card pickedCard = locals.getPickedCards().get(0).getCardObject();
                 System.out.println("Posilam serveru vybranou kartu: " + pickedCard.toOrdinal());
                 System.out.println(pickedCard);
                 server.sendAction(new PickedTrumpAction(pickedCard));
                 locals.getPickedCards().clear();
                 actionButtons.clear();
                 return;
            }
        }

        locals.getHandEntity().update(gc, null, delta);


        // Obsluha pohybu mysi nad tlacitkama
        controlActionButtons(input, actionButtons, allowedActions);
        updateActionButtons(actionButtons, gc, sb, delta);
        

    }

    public void render(GameContainer gc, StateBasedGame sb, Graphics gr) {
           locals.getHandEntity().render(gc, sb, gr);

           renderActionButtons(actionButtons, gc, sb, gr);


    }

    /** Akce zruseni vybrane karty. Znovu naplni frontu seriovych komponent a smaze vybrane karty. */
    @Override
    public void refusePickedCards() {

        serialComponents.clear();
        locals.getPickedCards().clear();
        try {
           this.addSerialComponent(new PickCardComponent(0, globals, locals, cardsPositions, locals.getCardsEntities()));
           this.addSerialComponent(new ConfirmPickedComponent(0, globals, locals, locals.getCardsEntities()));
           this.addSerialComponent(new GrabCardsComponent(0, cardsPositions, locals.getCardsEntities()));
        } catch (SlickException ex) {
           Logger.getLogger(PickTrumpPhase.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

 
}
